<template>
  <div>
    <!-- 面包屑导航区 -->
    <el-breadcrumb separator="/">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>企业管理</el-breadcrumb-item>
      <el-breadcrumb-item>企业列表</el-breadcrumb-item>
    </el-breadcrumb>

    <!-- 卡片视图区域 -->
    <el-card>
      <el-row :gutter="20">
        <el-col :span="8">
          <el-input placeholder="请输入内容" clearable>
            <el-button slot="append" icon="el-icon-search"></el-button>
          </el-input>
        </el-col>
        <el-col :span="4">
          <el-button type="primary" @click="addDialogVisible = true"
            >添加企业</el-button
          >
        </el-col>
      </el-row>

      <!-- 用户列表区 -->
      <el-table border stripe :data="getbusinesslist">
        <el-table-column type="index"></el-table-column>
        <el-table-column
          label="企业编号"
          prop="enterpriseNumber"
        ></el-table-column>
        <el-table-column label="密码" prop="password"></el-table-column>
        <el-table-column
          label="企业名称"
          prop="enterpriseName"
        ></el-table-column>
        <el-table-column label="负责人" prop="leader"></el-table-column>
        <el-table-column label="联系电话" prop="phone"></el-table-column>
        <el-table-column label="企业邮箱" prop="email"></el-table-column>
        <el-table-column
          label="企业介绍"
          prop="enterpriseIntroduction"
        ></el-table-column>
        <el-table-column label="操作" width="180px">
          <template slot-scope="scope">
            <el-button type="info" icon="el-icon-share" size="mini"></el-button>
            <el-button
              type="primary"
              icon="el-icon-edit"
              size="mini"
              @click="showEditDialog(scope.row.id)"
            ></el-button>
            <el-button
              type="danger"
              icon="el-icon-delete"
              size="mini"
              @click="removeUserById(scope.row.id)"
            ></el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>

    <!-- 添加用户 -->
    <el-dialog
      @close="addDialogClosed"
      title="添加企业"
      :visible.sync="addDialogVisible"
      width="50%"
    >
      <el-form
        :model="addForm"
        :rules="addFormRules"
        ref="addFormRef"
        label-width="80px"
      >
        <el-form-item label="企业编号" prop="userName">
          <el-input v-model="addForm.userName"></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input v-model="addForm.password"></el-input>
        </el-form-item>
        <el-form-item label="企业名称" prop="enterpriseName">
          <el-input v-model="addForm.enterpriseName"></el-input>
        </el-form-item>
        <el-form-item label="负责人" prop="leader">
          <el-input v-model="addForm.leader"></el-input>
        </el-form-item>
        <el-form-item label="联系电话" prop="phone">
          <el-input v-model="addForm.phone"></el-input>
        </el-form-item>
        <el-form-item label="企业邮箱" prop="email">
          <el-input v-model="addForm.email"></el-input>
        </el-form-item>
        <el-form-item label="企业介绍" prop="enterpriseIntroduction">
          <el-input
            type="textarea"
            :autosize="{ minRows: 1, maxRows: 4 }"
            placeholder="请输入内容"
            v-model="addForm.enterpriseIntroduction"
          >
          </el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="addDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="addBusiness">确 定</el-button>
      </span>
    </el-dialog>
    <!-- 修改企业用户信息弹框 -->
    <el-dialog
      @close="showDialogClosed"
      title="修改企业"
      :visible.sync="editDialogVisible"
      width="50%"
    >
      <el-form
        :model="editForm"
        :rules="addFormRules"
        ref="editFormRef"
        label-width="80px"
      >
        <el-form-item label="企业编号" prop="userName">
          <el-input v-model="editForm.userName"></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input v-model="editForm.password"></el-input>
        </el-form-item>
        <el-form-item label="企业名称" prop="enterpriseName">
          <el-input v-model="editForm.enterpriseName"></el-input>
        </el-form-item>
        <el-form-item label="负责人" prop="leader">
          <el-input v-model="editForm.leader"></el-input>
        </el-form-item>
        <el-form-item label="联系电话" prop="phone">
          <el-input v-model="editForm.phone"></el-input>
        </el-form-item>
        <el-form-item label="企业邮箱" prop="email">
          <el-input v-model="editForm.email"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="editDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="editBusiness">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data() {
    //验证邮箱的规则
    var checkEmail = (rule, value, cb) => {
      const regEmail = /^\w+@\w+(\.\w+)+$/;
      if (regEmail.test(value)) {
        return cb();
      }
      //返回一个错误提示
      cb(new Error("请输入合法的邮箱"));
    };
    //验证电话号码的规则
    var checkMobile = (rule, value, cb) => {
      const regMobile = /^1[3|4|5|7|8][0-9]{9}$/;
      if (regMobile.test(value)) {
        return cb();
      }
      //返回一个错误提示
      cb(new Error("请输入合法的手机号码"));
    };
    return {
      //添加表单数据
      addForm: {
        userName: "",
        password: "",
        enterpriseName: "",
        leader: "",
        phone: "",
        email: "",
        enterpriseIntroduction: "",
      },
      //获取企业数据列表
      getbusinesslist: [],
      //控制添加对话框的显示与隐藏
      addDialogVisible: false,
      //表单域
      textarea2: "",
      //添加表单验证规则
      addFormRules: {
        userName: [
          {
            required: true,
            message: "请输入企业编号",
            trigger: "blur",
          },
          {
            min: 4,
            max: 10,
            message: "企业编号的长度在4~10个字符之间",
            trigger: "blur",
          },
        ],
        password: [
          {
            required: true,
            message: "请输入密码",
            trigger: "blur",
          },
          {
            min: 6,
            max: 15,
            message: "企业密码的长度在6~15个字符之间",
            trigger: "blur",
          },
        ],
        enterpriseName: [
          {
            required: true,
            message: "请输入企业名称",
            trigger: "blur",
          },
          {
            min: 2,
            max: 15,
            message: "企业名称的长度在2~15个字符之间",
            trigger: "blur",
          },
        ],
        leader: [
          {
            required: true,
            message: "请输入负责人名称",
            trigger: "blur",
          },
          {
            min: 2,
            max: 10,
            message: "负责人名称的长度在2~10个字符之间",
            trigger: "blur",
          },
        ],
        phone: [
          {
            required: true,
            message: "请输入手机号码",
            trigger: "blur",
          },
          {
            validator: checkMobile,
            message: "手机号码不正确, 请重新输入",
            trigger: "blur",
          },
        ],
        email: [
          {
            required: true,
            message: "请输入邮箱",
            trigger: "blur",
          },
          {
            validator: checkEmail,
            message: "邮箱格式不正确，请重新输入",
            trigger: "blur",
          },
        ],
        enterpriseIntroduct: [
          {
            required: true,
            message: "请输入企业介绍",
            trigger: "blur",
          },
          {
            min: 2,
            max: 100,
            message: "企业介绍的长度在2~100个字符之间",
            trigger: "blur",
          },
        ],
      },
      editDialogVisible: false,
      editForm: {},
    };
  },

  methods: {
    async getBusinessForm() {
      const { data: res } = await this.$http.get("/Admin/enterprises");
      console.log(res);
      if (res.code !== 2002) {
        this.$message.error("获取企业数据列表失败！");
      } else {
        this.getbusinesslist = res.objData;
      }
    },
    //控制添加表单显示与隐藏
    // addDialogVisible() {},

    // 用户编辑框显示
    async showEditDialog(id) {
      this.editDialogVisible = true;
      //   console.log(id);
      const { data: res } = await this.$http.get("Admin/enterprises/" + id);

      if (res.code !== 2002) {
        return this.$message.error("查询用户信息失败！");
      }
      this.editForm = res.objData;
      //   this.editDialogVisible = true;
    },
    // 编辑内容提交保存
    editBusiness() {
      console.log(this.editForm);
      let {
        id,
        userName,
        password,
        enterpriseName,
        leader,
        phone,
        email,
        enterpriseIntroduction,
      } = this.editForm;
      this.$refs.editFormRef.validate(async (valid) => {
        if (!valid) return;
        //发起修改用户信息的请求
        const { data: res } = await this.$http.put("Admin/enterprise/", {
          id: id,
          userName: userName,
          password: password,
          enterpriseName: enterpriseName,
          leader: leader,
          phone: phone,
          email: email,
          enterpriseIntroduction: enterpriseIntroduction,
        });
        if (res.code !== 2002) {
          return this.$message.error("更新用户信息失败！");
        }
        //关闭对话框
        this.editDialogVisible = false;
        console.log(this.editDialogVisible);
        //重新渲染用户列表
        this.getBusinessForm();
        //更新成功提醒信息
        this.$message.success("更新用户信息成功！");
      });
    },
    // 关闭对话框后重置表单
    addDialogClosed() {
      this.$refs.addFormRef.resetFields();
    },
    // 关闭编辑对话框重置
    showDialogClosed() {
      this.$refs.editFormRef.resetFields();
    },
    //添加企业信息
    addBusiness() {
      console.log("1");
      this.$refs.addFormRef.validate(async (valid) => {
        if (!valid) return;
        console.log("2");
        const { data: res } = await this.$http.post(
          "/Admin/enterprise",
          this.addForm
        );
        if (res.code !== 2002) {
          this.$message.error("添加企业数据失败！");
        } else {
          this.$message.success("添加企业数据成功！");
          this.addDialogVisible = false;
          this.getBusinessForm();
        }
      });
    },
    // 企业信息删除
    async removeUserById(id) {
      let data = {
        id: id,
      };
      //弹框询问用户是否删除
      const confirmResult = await this.$confirm(
        "此操作将永久删除该用户，是否继续？",
        "提示",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }
      ).catch((err) => err);
      //如果用户确认删除，则返回值是comfirm
      //如果用户取消删除，则返回值是cancel
      if (confirmResult !== "confirm") {
        return this.$message.info("已取消删除");
      }
      console.log(JSON.stringify(data));
      //   console.log(data);
      const { data: res } = await this.$http.delete("/Admin/enterprise", {
        data: data,
      });
      console.log(res);
      if (res.code !== 2002) {
        return this.$message.error("删除用户失败！");
      }

      this.$message.success("删除用户成功！");
      this.getBusinessForm();
    },
  },
  created() {
    this.getBusinessForm();
  },
};
</script>

<style lang="less" scoped>
.el-card {
  margin-top: 20px;
}
.el-table {
  margin-top: 10px;
}
</style>